package com.prs.crm.action.doc;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.doc.DeptDocType;
import com.prs.crm.domain.doc.FormworkType;
import com.prs.crm.service.doc.FormworkTypeService;
import com.prs.crm.service.sys.LogService;

public class FormworkTypeAction extends BaseAction {
	private FormworkType formworkType;
	private FormworkTypeService formworkTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Collection<TreeNode> nodes;
	private LogService logService;

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	@Action("listFormworkType")
	public String listFormworkType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				FormworkType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFormworkTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "loadFormworkTypeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadFormworkTypeTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<FormworkType> formworkTypes = this.getFormworkTypeService()
				.getAllFormworkType();
		for (FormworkType formworkType : formworkTypes) {
			TreeNode node = new TreeNode();
			node.setId(formworkType.getId().toString());
			node.setText(formworkType.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}

	@Action("editFormworkType")
	public String editFormworkType() {
		return SUCCESS;
	}

	@Action(value = "saveFormworkType", results = {
			@Result(name = "success", type = "redirect", location = "listFormworkType"),
			@Result(name = "error", type = "chain", location = "editFormworkType") })
	public String saveFormworkType() {
		if (formworkType.getName() == null
				|| "".equals(formworkType.getName().trim())) {
			addActionError("类型名称不能为空");
			return ERROR;
		}
		if (this.getLogService()
				.getSearchName(FormworkType.class, formworkType.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getFormworkTypeService().save(formworkType);
		return SUCCESS;
	}

	@Action(value = "removeFormworkType", results = {
			@Result(name = "success", type = "redirect", location = "listFormworkType"),
			@Result(name = "error", type = "chain", location = "editFormworkType") })
	public String removeFormworkType() {
		if (ids == null) {
			this.addActionError("请选择要删除的模板类型");
			return ERROR;
		}
		for (Integer id : ids) {
			FormworkType type = this.getFormworkTypeService().get(id);
			if (type.getFormworks().size() != 0) {
				this.addActionError("已有模板引用，不能删除");
				return ERROR;
			}
		}
		this.getFormworkTypeService().batchRemove(ids);
		return SUCCESS;
	}

	public FormworkType getFormworkType() {
		return formworkType;
	}

	public void setFormworkType(FormworkType formworkType) {
		this.formworkType = formworkType;
	}

	public FormworkTypeService getFormworkTypeService() {
		return formworkTypeService;
	}

	public void setFormworkTypeService(FormworkTypeService formworkTypeService) {
		this.formworkTypeService = formworkTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

}
